#define   _CRT_SECURE_NO_WARNINGS  1
#pragma warning(disable:6031)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
#include<assert.h>
typedef int NewType;
void Swap(NewType* x, NewType* y);
void SortUp(NewType* ptr, int child);
void SortDown(NewType* ptr, int size, int father);
void SortDown(NewType* ptr, int size, int father)
{
	int child = father * 2 + 1;
	while (child < size)
	{
		if (child + 1 < size && ptr[child] < ptr[child + 1])
		{
			child++;
		}
		if (ptr[child] > ptr[father])
		{
			Swap(&ptr[child], &ptr[father]);
			father = child;
			child = father * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void Swap(NewType* x, NewType* y)
{
	NewType z = *x;
	*x = *y;
	*y = z;
}
void SortUp(NewType* ptr, int child)
{
	int father = (child - 1) / 2;
	while (child > 0)
	{
		if (ptr[child] > ptr[father])
		{
			Swap(&ptr[child], &ptr[father]);
			child = father;
			father = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}

}
void fun(NewType* ptr, int size)
{
	/*for (int i = 1; i < size; i++)
	{
		SortUp(ptr, i);
	}
	int end = size - 1;
	while (end)
	{
		Swap(&ptr[end], &ptr[0]);
		SortDown(ptr, end--, 0);
	}*/
	for (int i = (size - 2) / 2; i >= 0; i--)
	{
		SortDown(ptr, size, i);
	}
	int end = size - 1;
	while (end)
	{
		Swap(&ptr[end], &ptr[0]);
		SortDown(ptr, end--, 0);
	}
}
int main()
{
	int arr[] = { 21,43,34,45,5,56,6 };
	fun(arr, sizeof(arr) / sizeof(arr[0]));
	for (int x = 0; x < sizeof(arr) / sizeof(NewType); x++)
	{
		printf("%d ", arr[x]);

	}

	return 0;
}